package networkLoader;

import network.Device;
import network.Network;
import network.Port;
import network.Rule;
import org.jgrapht.alg.util.Pair;

import java.io.*;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;

public class DeltaNetwork {

    public static void main(String[] args) throws IOException {
        String line = "+120.251.81.9/24,input_node,output_node,555";
        char ch = line.charAt(0);
        String[] tokens = line.split(","); // 0: prefix, 1: device, 2: egress, 3: priority

        int prefx = Integer.parseInt(tokens[0].split("/")[1]);
        long dstIp = 0;
        for (String str : tokens[0].substring(1).split("/")[0].split("\\.")) {
            dstIp = (dstIp << 8) + Long.parseLong(str);
        }

        System.out.println(ch);
        System.out.println(dstIp);
        System.out.println(prefx);
        System.out.println(tokens[1]);
        System.out.println(tokens[2]);
        System.out.println(tokens[3]);


        InputStream gzipStream = new GZIPInputStream(new FileInputStream("deltanet/deltanet-NSDI17-dataset.tar.gz"));
        BufferedReader br = new BufferedReader(new InputStreamReader(gzipStream));
        int cnt = 0;
        while ((line = br.readLine()) != null) {
            ch = line.charAt(0); cnt ++;
            if (ch != '+' && ch != '-') System.out.println(cnt + " : " + line);
        }
    }

    public static Network getNetwork(String name) throws IOException {
        if (name.equals("airtel1")) return findNetwork(name, 38101, 14193734);
        if (name.equals("airtel2")) return findNetwork(name, 14193734, 519442862);
        if (name.equals("berkley")) return findNetwork(name, 519442862, 545078666);
        if (name.equals("inet")) return findNetwork(name, 545078666, 794545778);
        // if (name.equals("switch4")) return findNetwork(name, );
        return null;
    }

    /**
     * line (l, r)
     */
    private static Network findNetwork(String name, int l, int r) throws IOException {
        InputStream gzipStream = new GZIPInputStream(new FileInputStream("deltanet/deltanet-NSDI17-dataset.tar.gz"));
        BufferedReader br = new BufferedReader(new InputStreamReader(gzipStream));

        String line; int cnt = 0;
        Network network = new Network(name);
        network.updateSequence = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            cnt ++;

            if (cnt >= r) break;

            if (l < cnt) {
                char ch = line.charAt(0);
                String[] tokens = line.split(","); // 0: prefix, 1: device, 2: egress, 3: priority

                int prefx = Integer.parseInt(tokens[0].split("/")[1]);
                long dstIp = 0;
                for (String str : tokens[0].substring(1).split("/")[0].split("\\.")) {
                    dstIp = (dstIp << 8) + Long.parseLong(str);
                }

                Device device = network.getDevice(tokens[1]);
                Port outPort = device.getPort(tokens[2]);
                outPort.setPeer(network.getDevice(tokens[2]).getPort("default"));
                int priority = Integer.parseInt(tokens[3]);

                Rule rule = new Rule(device, dstIp, prefx, priority, outPort);
                if (ch == '+') network.updateSequence.add(new Pair<>(true, rule));
                if (ch == '-') network.updateSequence.add(new Pair<>(false, rule));
            }
        }

        Device.cnt = 0;
        network.getAllDevices().forEach(device -> device.uid = Device.cnt++);
        return network;
    }
}
